package com.gjs.demo002;

import static com.gjs.demo002.R.id.activity_main_home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 点击事件实现跳转到webview页面
        findViewById(R.id.activity_main_wv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,WebViewActivity.class);
                startActivity(intent);
            }
        });

        // 点击事件，获取用户手机号
        findViewById(R.id.activity_main_get_phone_num).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,GetPhoneNumActivity.class);
                startActivity(intent);
            }
        });



        // 点击事件监听，生成列表
        findViewById(R.id.activity_main_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取用户输入的数量
                int num = 100;
                EditText numEditText = (EditText)findViewById(R.id.activity_main_edittext);
                if (numEditText.getText() == null || numEditText.getText().equals("")){
                    Toast.makeText(MainActivity.this,"没有输入",Toast.LENGTH_LONG);
                    num = 100;
                }else{
                    num = Integer.parseInt(numEditText.getText().toString());
                }
                Intent intent = new Intent();
                intent.putExtra("num",num);
                intent.setClass(MainActivity.this,ListViewActivity.class);
                startActivity(intent);
            }
        });
        // 跳转到recyclerview页面
        findViewById(R.id.activity_main_recyclerview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(MainActivity.this,MyRecyclerActivity.class);
                Intent intent = new Intent(MainActivity.this,RecyclerViewDemoActivity.class);
                startActivity(intent);
            }
        });
        // 进入首页直接进入
        Bundle bundle = new Bundle();
        bundle.putString("msg","需要传的值！");
        HomeFragment homeFragment = new HomeFragment();
        homeFragment.setArguments(bundle);
        replaceFragment(homeFragment);
        // framelayout按钮的点击事件
        Button homeBtn = findViewById(activity_main_home);
        homeBtn.setOnClickListener(this);
        Button userBtn = findViewById(R.id.activity_main_user);
        userBtn.setOnClickListener(this);
    }




    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.activity_main_home: replaceFragment(new HomeFragment());break;
//            case R.id.activity_main_user: replaceFragment(new UserFragment());break;
//        }

        Bundle bundle = new Bundle();
        if (R.id.activity_main_home == v.getId()){
            bundle.putString("msg","需要传的值！");
            HomeFragment homeFragment = new HomeFragment();
            homeFragment.setArguments(bundle);
            replaceFragment(homeFragment);
        } else if (R.id.activity_main_user == v.getId()) {
            replaceFragment(new UserFragment());
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.activity_main_framelayout,fragment);
        fragmentTransaction.addToBackStack(null); // 添加到栈中
        fragmentTransaction.commit();
    }
}